package com.yuncheng.spcyApi.busService;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.other.DaConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyDaSqlcMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.Result;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 *
 */
@Component(value = "spcyDaService")
public class SpcyDaService {
    private static final Logger log = LoggerFactory.getLogger(SpcyDaService.class);

    @Resource
    @Lazy
    private ISpcyDaWjjlService spcyDaWjjlService;

    @Resource
    @Lazy
    private ISpcyDaCkrkjlService spcyDaCkrkjlService;

    @Resource
    @Lazy
    private ISpcyDaDzdakService spcyDaDzdakService;

    @Resource
    @Lazy
    private ISpcyDaZzdakService spcyDaZzdakService;

    @Resource
    @Lazy
    private ISpcyDaSqlcService spcyDaSqlcService;

    @Resource
    @Lazy
    private SpcyDaSqlcMapper spcyDaSqlcMapper;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private SpcyMinioUtils spcyMinioUtils;


    /**
     * 获取当前登录人
     * @return
     */
    public CurrentUserVo getNowUser(){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            throw new BusinessException("当前登录已失效，请重新登录!");
        }
        return currentUser;
    }

    /**
     * 查询入库-出库记录
     */
    public HttpResult queryCkrkjlPageList(SpcyDaCkrkjl ckrkjl, Integer pageNo, Integer pageSize){
        Page<SpcyDaCkrkjl> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyDaCkrkjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(ckrkjl.getFjllx()),SpcyDaCkrkjl::getFjllx,ckrkjl.getFjllx());
        queryWrapper.eq(StringUtils.isNotBlank(ckrkjl.getFdabh()),SpcyDaCkrkjl::getFdabh,ckrkjl.getFdabh());
        queryWrapper.eq(StringUtils.isNotBlank(ckrkjl.getFdamc()),SpcyDaCkrkjl::getFdamc,ckrkjl.getFdamc());
        queryWrapper.eq(StringUtils.isNotBlank(ckrkjl.getFczr()),SpcyDaCkrkjl::getFczr,ckrkjl.getFczr());
        queryWrapper.eq(StringUtils.isNotBlank(ckrkjl.getFczrbm()),SpcyDaCkrkjl::getFczrbm,ckrkjl.getFczrbm());
        queryWrapper.eq(StringUtils.isNotBlank(ckrkjl.getFdaid()),SpcyDaCkrkjl::getFdaid,ckrkjl.getFdaid());
        queryWrapper.eq(StringUtils.isNotBlank(ckrkjl.getId()),SpcyDaCkrkjl::getId,ckrkjl.getId());
        queryWrapper.orderByDesc(SpcyDaCkrkjl::getFczrq);
        queryWrapper.orderByDesc(SpcyDaCkrkjl::getCreateTime);
        IPage<SpcyDaCkrkjl> pageList = spcyDaCkrkjlService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    /**
     * 添加入库记录
     * @param daid 档案id
     * @param dabh 档案编号
     * @param damc 档案名称
     * @param fbz 备注
     * @return
     */
    public Result addRkjl(String daid,String dabh,String damc,String fbz,SpcyDaSqlc sqlc){
        SpcyDaCkrkjl ckrkjl = new SpcyDaCkrkjl();
        ckrkjl.setId(GetUuIdUtils.ReplaceUuId());
        ckrkjl.setFjllx("入库");
        ckrkjl.setFdabh(dabh);
        ckrkjl.setFdamc(damc);
        ckrkjl.setFdaid(daid);

        String msg = "";
        if (StringUtils.isNotBlank(fbz)){
            msg += fbz + "\n";
        }

        // 申请流程内容
        if (sqlc != null){
            ckrkjl.setFczr(sqlc.getFsqrmc());
            if (DateConversionUtils.isValidDateStringYmd(sqlc.getFsqrq())) {
                ckrkjl.setFczrq(DateConversionUtils.convertStringToDateYmd(sqlc.getFsqrq()));
            }else {
                ckrkjl.setFczrq(new Date());
            }
            ckrkjl.setFczrbm(sqlc.getFsqrbm());

            // 申请内容
            String lognr = LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(sqlc));
            msg += lognr;

        }else {
            CurrentUserVo nowUser = this.getNowUser();
            ckrkjl.setFczr(nowUser.getName());
            ckrkjl.setFczrbm(nowUser.getOrgName());
            ckrkjl.setFczrq(new Date());

            msg += "[档案负责人]-手动入库。";
        }

        if (StringUtils.isNotBlank(msg)) {
            ckrkjl.setFbz(msg);
        }

        spcyDaCkrkjlService.save(ckrkjl);

        return Result.ok("操作成功");
    }

    /**
     * 添加出库记录
     * @param daid 档案id
     * @param dabh 档案编号
     * @param damc 档案名称
     * @param fbz 备注
     * @return
     */
    public Result addCkjl(String daid,String dabh,String damc,String fbz,SpcyDaSqlc sqlc){

        SpcyDaCkrkjl ckrkjl = new SpcyDaCkrkjl();
        ckrkjl.setId(GetUuIdUtils.ReplaceUuId());
        ckrkjl.setFjllx("出库");
        ckrkjl.setFdabh(dabh);
        ckrkjl.setFdamc(damc);
        ckrkjl.setFbz(fbz);
        ckrkjl.setFdaid(daid);

        String msg = "";
        if (StringUtils.isNotBlank(fbz)){
            msg += fbz + "\n";
        }

        // 申请流程内容
        if (sqlc != null){
            ckrkjl.setFczr(sqlc.getFsqrmc());
            if (DateConversionUtils.isValidDateStringYmd(sqlc.getFsqrq())) {
                ckrkjl.setFczrq(DateConversionUtils.convertStringToDateYmd(sqlc.getFsqrq()));
            }else {
                ckrkjl.setFczrq(new Date());
            }
            ckrkjl.setFczrbm(sqlc.getFsqrbm());

            // 申请内容
            String lognr = LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(sqlc));
            msg += lognr;

        }else {
            CurrentUserVo nowUser = this.getNowUser();
            ckrkjl.setFczr(nowUser.getName());
            ckrkjl.setFczrbm(nowUser.getOrgName());
            ckrkjl.setFczrq(new Date());
        }

        if (StringUtils.isNotBlank(msg)) {
            ckrkjl.setFbz(msg);
        }

        spcyDaCkrkjlService.save(ckrkjl);

        return Result.ok("操作成功");
    }

    /**
     * 查询电子档案-分页查询
     * @param dzdak
     * @param pageNo
     * @param pageSize
     * @return
     */
    public HttpResult queryDzdaPageList(SpcyDaDzdak dzdak, Integer pageNo, Integer pageSize){
        Page<SpcyDaDzdak> page = new Page<>(pageNo, pageSize);

        /*LambdaQueryWrapper<SpcyDaDzdak> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(dzdak.getFdabh()),SpcyDaDzdak::getFdabh,dzdak.getFdabh());
        queryWrapper.eq(StringUtils.isNotBlank(dzdak.getFdamc()),SpcyDaDzdak::getFdamc,dzdak.getFdamc());
        queryWrapper.eq(StringUtils.isNotBlank(dzdak.getFflmc()),SpcyDaDzdak::getFflmc,dzdak.getFflmc());
        queryWrapper.eq(StringUtils.isNotBlank(dzdak.getFsfgd()),SpcyDaDzdak::getFsfgd,dzdak.getFsfgd());
        queryWrapper.eq(StringUtils.isNotBlank(dzdak.getFbmdj()),SpcyDaDzdak::getFbmdj,dzdak.getFbmdj());
        queryWrapper.eq(StringUtils.isNotBlank(dzdak.getFbmnx()),SpcyDaDzdak::getFbmnx,dzdak.getFbmnx());
        queryWrapper.eq(StringUtils.isNotBlank(dzdak.getFzzdaid()),SpcyDaDzdak::getFzzdaid,dzdak.getFzzdaid());
        queryWrapper.eq(StringUtils.isNotBlank(dzdak.getFbz()),SpcyDaDzdak::getFbz,dzdak.getFbz());
        if (dzdak.getFyxzt() != null){
            queryWrapper.eq(SpcyDaDzdak::getFyxzt,dzdak.getFyxzt());
        }else {
            queryWrapper.eq(SpcyDaDzdak::getFyxzt,YXZT_YX);
        }
        queryWrapper.orderByAsc(SpcyDaDzdak::getFdabh);
        queryWrapper.orderByDesc(SpcyDaDzdak::getCreateTime);
        IPage<SpcyDaDzdak> pageList = spcyDaDzdakService.page(page, queryWrapper);*/
        dzdak.setFyxzt(DaConstant.YXZT_YX);
        IPage<SpcyDaDzdak> pageList = spcyDaSqlcMapper.selectDzdaPageList(page, dzdak);
        return HttpResult.ok(pageList);
    }

    /**
     * 查询纸质档案-分页查询
     * @param zzdak
     * @param pageNo
     * @param pageSize
     * @return
     */
    public HttpResult queryZzdaPageList(SpcyDaZzdak zzdak, Integer pageNo, Integer pageSize){
        Page<SpcyDaZzdak> page = new Page<>(pageNo, pageSize);
        /*LambdaQueryWrapper<SpcyDaZzdak> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(zzdak.getFbmdj()),SpcyDaZzdak::getFbmdj,zzdak.getFbmdj());
        queryWrapper.eq(StringUtils.isNotBlank(zzdak.getFbmnx()),SpcyDaZzdak::getFbmnx,zzdak.getFbmnx());
        queryWrapper.eq(StringUtils.isNotBlank(zzdak.getFbz()),SpcyDaZzdak::getFbz,zzdak.getFbz());
        queryWrapper.eq(StringUtils.isNotBlank(zzdak.getFdabh()),SpcyDaZzdak::getFdabh,zzdak.getFdabh());
        queryWrapper.eq(StringUtils.isNotBlank(zzdak.getFdamc()),SpcyDaZzdak::getFdamc,zzdak.getFdamc());
        queryWrapper.eq(StringUtils.isNotBlank(zzdak.getFdasmc()),SpcyDaZzdak::getFdasmc,zzdak.getFdasmc());
        queryWrapper.eq(StringUtils.isNotBlank(zzdak.getFdzdaid()),SpcyDaZzdak::getFdzdaid,zzdak.getFdzdaid());
        queryWrapper.eq(StringUtils.isNotBlank(zzdak.getFflmc()),SpcyDaZzdak::getFflmc,zzdak.getFflmc());
        queryWrapper.eq(StringUtils.isNotBlank(zzdak.getFrkzt()),SpcyDaZzdak::getFrkzt,zzdak.getFrkzt());
        if (zzdak.getFyxzt() != null){
            queryWrapper.eq(SpcyDaZzdak::getFyxzt,zzdak.getFyxzt());
        }else {
            queryWrapper.eq(SpcyDaZzdak::getFyxzt,YXZT_YX);
        }
        queryWrapper.orderByAsc(SpcyDaZzdak::getFdabh);
        queryWrapper.orderByDesc(SpcyDaZzdak::getCreateTime);
        IPage<SpcyDaZzdak> pageList = spcyDaZzdakService.page(page, queryWrapper);*/
        zzdak.setFyxzt(DaConstant.YXZT_YX);
        IPage<SpcyDaZzdak> pageList = spcyDaSqlcMapper.selectZzdaPageList(page, zzdak);
        return HttpResult.ok(pageList);
    }

    /**
     * 申请数据查询 - 借阅查询
     * @param onlyCurrentUser 是否查询个人
     */
    public HttpResult queryDaSqlcPageList(SpcyDaSqlc sqlc,Integer pageNo, Integer pageSize,boolean onlyCurrentUser){
        Page<SpcyDaSqlc> page = new Page<>(pageNo, pageSize);
        if (onlyCurrentUser){
            sqlc.setFsqrid(this.getNowUser().getId());
        }
        IPage<SpcyDaSqlc> pageList = spcyDaSqlcMapper.selectDaSqlcPageList(page, sqlc);
        return HttpResult.ok(pageList);
    }

    /**
     * 查询申请借阅档案 - 申请数据-申请id
     * @param sqid
     * @return
     */
    public SpcyDaSqlc querySqlcBySqid(String sqid){
        SpcyDaSqlc sqlc = spcyDaSqlcService.getById(sqid);

        return sqlc;
    }

    /**
     * 电子档案-申请-借阅
     */
    public Result addJysqByDzda(SpcyDaSqlc sqlc){
        String sqid = "";
        String sqbh = "";
        boolean isUpdate = false;
        if (StringUtils.isNotBlank(sqlc.getId())){
            isUpdate = true;
            sqid = sqlc.getId();
        }else {
            sqid = GetUuIdUtils.ReplaceUuId();
        }

        if (StringUtils.isBlank(sqlc.getFdaid())){
            return Result.error("请选择要申请的档案!");
        }
        if (StringUtils.isBlank(sqlc.getFjyzt())){
            return Result.error("借阅状态不能为空！");
        }
        SpcyDaDzdak dzda = spcyDaDzdakService.getById(sqlc.getFdaid());
        if (dzda == null){
            return Result.error("当前申请的档案不存在，请刷新重试！");
        }
        if (StringUtils.isBlank(sqlc.getFsqbh())){
            sqbh = this.getJysqBhgz(); // 获取申请编号
        }else {
            sqbh =  sqlc.getFsqbh();
        }

        CurrentUserVo nowUser = this.getNowUser();
        sqlc.setId(sqid);
        sqlc.setFsqbh(sqbh);
        sqlc.setFsqrid(nowUser.getId());
        sqlc.setFsqrmc(nowUser.getName());
        sqlc.setFsqrbm(nowUser.getOrgName());
        sqlc.setFsqrq(DateConversionUtils.DateToDays(new Date()));
        sqlc.setFflmc(dzda.getFflmc());
        sqlc.setFdabh(dzda.getFdabh());
        sqlc.setFdamc(dzda.getFdamc());
        sqlc.setFspzt(DaConstant.SQ_SPZT_SQ);

        // 存在纸质 - 需要有借出 操作
        if (sqlc.getFjyzt().contains(DaConstant.SQ_XT_ZZ)){
            // 判断是否有纸质档案 - 有-借阅状态-待借出，否则为 无需借出
            SpcyDaZzdak zzda = this.getZzdaByDzdaId(sqlc.getFdaid());
            if (zzda != null){
                sqlc.setFjyzt(DaConstant.SQ_JYZT_DJC);
            }else {
                sqlc.setFjyzt(DaConstant.SQ_JYZT_WXJC);
            }
        }else {
            sqlc.setFjyzt(DaConstant.SQ_JYZT_WXJC);
        }

        if (isUpdate) {
            spcyDaSqlcService.updateById(sqlc);
        }else {
            spcyDaSqlcService.save(sqlc);
        }

        String lognr = "档案申请借阅：";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(sqlc));
        spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM+"-档案借阅",lognr,sqlc.getFdaid(),SpcyConstant.LOGS_SYS_TYPE_BC,SpcyConstant.LOGS_SYS_TYPE_BC);

        Result result = new Result(true);
        result.setResult(sqlc);
        result.setMessage("申请成功");
        return result;
    }

    /**
     * 电子档案-申请-借阅-更新
     */
    public Result updateJysqByDzda(HashMap<String,String> buffer)throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return Result.error("信息id不能为空");
        }
        String id = buffer.get("id");
        SpcyDaDzdak Update = spcyDaDzdakService.getById(id);
        if (Update == null){
            return Result.error("当前基本信息数据不存在，请重试");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(Update);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(Update, buffer);
        spcyDaDzdakService.updateById(Update);

        String operContent = "电子档案-申请-借阅-修改：";
        operContent += LogUtils.BeanModifyMsg(Update,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM+"-档案借阅",operContent,id,SpcyConstant.LOGS_SYS_TYPE_XG,SpcyConstant.LOGS_SYS_TYPE_XG);

        return Result.ok("保存成功");
    }

    /**
     * 更新-档案申请-状态
     * @param sqid 申请id
     * @param jyzt 借阅状态（待借出/待用户归还/确认归还/待确认归还/已归还）
     * @param spzt 审批状态（申请/无审批/待审批/已通过）
     */
    public Result updateJysqStateByDzda(String sqid,String jyzt,String spzt){
        SpcyDaSqlc sqlc = spcyDaSqlcService.getById(sqid);
        if (sqlc == null){
            throw new BusinessException("当前档案数据不存在，请刷新重试！");
        }
        if (StringUtils.isNotBlank(jyzt)) {
            sqlc.setFjyzt(jyzt);
        }
        if (StringUtils.isNotBlank(spzt)){
            sqlc.setFspzt(spzt);
        }
        spcyDaSqlcService.updateById(sqlc);

        return Result.ok("保存成功");
    }

    /**
     * 添加电子档案
     * 默认: 预归档，绝密
     * @return
     */
    public HttpResult addDzda(SpcyDaDzdak dzdak){
        if (StringUtils.isBlank(dzdak.getFdabh())){
            return HttpResult.error("档案编号不能为空！");
        }
        if (StringUtils.isBlank(dzdak.getFdamc())){
            return HttpResult.error("档案名称不能为空！");
        }

        // 判断纸质档案是否已经绑定
        if (StringUtils.isNotBlank(dzdak.getFzzdaid())){
            Result result = verifyDzdaBdZzda(dzdak.getFzzdaid());
            if (!result.isSuccess()){
                return HttpResult.error(result.getMessage());
            }
        }

        String daid = GetUuIdUtils.ReplaceUuId();
        dzdak.setId(daid);
        dzdak.setFyxzt(DaConstant.YXZT_YX);
        dzdak.setFsfgd(DaConstant.DZDA_GDZT_NO);
        if (StringUtils.isBlank(dzdak.getFbmdj())) {
            dzdak.setFbmdj(DaConstant.DA_BMDJ_JUEM);
        }
        spcyDaDzdakService.save(dzdak);

        String lognr = "添加电子档案：";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(dzdak));
        spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM,lognr,daid, SpcyConstant.LOGS_SYS_TYPE_XZ,SpcyConstant.LOGS_SYS_TYPE_XZ);

        HttpResult result = new HttpResult(true);
        result.setMessage("保存成功");
        result.setResult(dzdak);
        return result;
    }

    /**
     * 添加纸质档案
     * 默认：预入库、绝密
     * @return
     */
    public HttpResult addZzda(SpcyDaZzdak zzdak){
        if (StringUtils.isBlank(zzdak.getFdabh())){
            return HttpResult.error("档案编号不能为空！");
        }
        if (StringUtils.isBlank(zzdak.getFdamc())){
            return HttpResult.error("档案名称不能为空！");
        }

        // 判断电子档案是否已经绑定
        if (StringUtils.isNotBlank(zzdak.getFdzdaid())){
            Result result = verifyZzdaBdDzda(zzdak.getFdzdaid());
            if (!result.isSuccess()){
                return HttpResult.error(result.getMessage());
            }
        }

        String daid = GetUuIdUtils.ReplaceUuId();
        zzdak.setId(daid);
        zzdak.setFyxzt(DaConstant.YXZT_YX);
        zzdak.setFrkzt(DaConstant.ZZDA_RKZT_YRK);
        if (StringUtils.isBlank(zzdak.getFbmdj())) {
            zzdak.setFbmdj(DaConstant.DA_BMDJ_JUEM);
        }
        spcyDaZzdakService.save(zzdak);

        String lognr = "添加纸质档案：";
        lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(zzdak));
        spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM,lognr,daid, SpcyConstant.LOGS_SYS_TYPE_XZ,SpcyConstant.LOGS_SYS_TYPE_XZ);

        HttpResult result = new HttpResult(true);
        result.setMessage("保存成功");
        result.setResult(zzdak);
        return result;
    }

    /**
     * 更新电子档案
     * @return
     */
    public HttpResult updateDzda(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("信息id不能为空");
        }
        String id = buffer.get("id");
        SpcyDaDzdak Update = spcyDaDzdakService.getById(id);
        if (Update == null){
            return HttpResult.error("当前基本信息数据不存在，请重试");
        }

        String oldZzdaid = Update.getFzzdaid(); // 旧的纸质档案id

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(Update);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(Update, buffer);

        String newZzdaid = Update.getFzzdaid(); // 新的纸质档案id

        /** 判断纸质档案是否已经绑定 */
        if (StringUtils.isNotBlank(oldZzdaid) && StringUtils.isNotBlank(newZzdaid) && oldZzdaid.equals(newZzdaid)){
            // 旧的纸质档案和新的纸质档案相同不做处理

        }
        else if (StringUtils.isBlank(oldZzdaid) && StringUtils.isNotBlank(newZzdaid)){
            // 判断纸质档案是否已经绑定
            Result result = verifyDzdaBdZzda(newZzdaid);
            if (!result.isSuccess()){
                return HttpResult.error(result.getMessage());
            }
        }
        else if (StringUtils.isNotBlank(oldZzdaid) && StringUtils.isBlank(newZzdaid)){
            // 旧的纸质档案不为空，新的档案为空，不做处理

        }else if (StringUtils.isNotBlank(oldZzdaid) && StringUtils.isNotBlank(newZzdaid) && !oldZzdaid.equals(newZzdaid)){
            // 判断纸质档案是否已经绑定
            Result result = verifyDzdaBdZzda(newZzdaid);
            if (!result.isSuccess()){
                return HttpResult.error(result.getMessage());
            }

        }

        spcyDaDzdakService.updateById(Update);

        String operContent = "更新电子档案-修改：";
        operContent += LogUtils.BeanModifyMsg(Update,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM,operContent,id,SpcyConstant.LOGS_SYS_TYPE_XG,SpcyConstant.LOGS_SYS_TYPE_XG);

        return HttpResult.ok("保存成功");
    }

    /**
     * 更新纸质档案
     * @return
     */
    public HttpResult updateZzda(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("信息id不能为空");
        }
        String id = buffer.get("id");
        SpcyDaDzdak Update = spcyDaDzdakService.getById(id);
        if (Update == null){
            return HttpResult.error("当前基本信息数据不存在，请重试");
        }

        String oldDzdaid = Update.getFzzdaid(); // 旧的电子档案id

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(Update);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(Update, buffer);

        String newDzdaid = Update.getFzzdaid(); // 新的电子档案id

        /** 判断纸质档案是否已经绑定 */
        if (StringUtils.isNotBlank(oldDzdaid) && StringUtils.isNotBlank(newDzdaid) && oldDzdaid.equals(newDzdaid)){
            // 旧的电子档案和新的电子档案相同不做处理

        }
        else if (StringUtils.isBlank(oldDzdaid) && StringUtils.isNotBlank(newDzdaid)){
            // 判断电子档案是否已经绑定
            Result result = verifyZzdaBdDzda(newDzdaid);
            if (!result.isSuccess()){
                return HttpResult.error(result.getMessage());
            }

        }
        else if (StringUtils.isNotBlank(oldDzdaid) && StringUtils.isBlank(newDzdaid)){
            // 旧的电子档案不为空，新的电子档案为空，不做处理

        }else if (StringUtils.isNotBlank(oldDzdaid) && StringUtils.isNotBlank(newDzdaid) && !oldDzdaid.equals(newDzdaid)){
            // 判断电子档案是否已经绑定
            Result result = verifyZzdaBdDzda(newDzdaid);
            if (!result.isSuccess()){
                return HttpResult.error(result.getMessage());
            }

        }

        spcyDaDzdakService.updateById(Update);

        String operContent = "更新纸质档案-修改：";
        operContent += LogUtils.BeanModifyMsg(Update,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM,operContent,id,SpcyConstant.LOGS_SYS_TYPE_XG,SpcyConstant.LOGS_SYS_TYPE_XG);

        return HttpResult.ok("保存成功");
    }

    /**
     * 删除电子档案
     */
    public HttpResult deleteDzda(String ids){
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(",|，"));
        List<SpcyDaDzdak> beanList = spcyDaDzdakService.listByIds(idsList);
        if (CollectionUtils.isEmpty(beanList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyDaDzdak bean : beanList){
            bean.setFyxzt(DaConstant.YXZT_YSX); // 失效
            spcyDaDzdakService.updateById(bean);

            operContent = "删除电子档案基本信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(bean);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM,operContent,bean.getId(),SpcyConstant.LOGS_SYS_TYPE_SC,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";
        }

        return HttpResult.ok("删除成功");
    }

    /**
     * 删除纸质档案
     */
    public HttpResult deleteZzda(String ids){
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(",|，"));
        List<SpcyDaZzdak> beanList = spcyDaZzdakService.listByIds(idsList);
        if (CollectionUtils.isEmpty(beanList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyDaZzdak bean : beanList){
            bean.setFyxzt(DaConstant.YXZT_YSX); // 失效
            spcyDaZzdakService.updateById(bean);

            operContent = "删除纸质档案基本信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(bean);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM,operContent,bean.getId(),SpcyConstant.LOGS_SYS_TYPE_SC,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";
        }

        return HttpResult.ok("删除成功");
    }

    /**
     * 判断电子档案绑定的纸质档案是否已经并绑定
     */
    public Result verifyDzdaBdZzda(String zzdaid){
        List<SpcyDaDzdak> bdzzdaList = spcyDaDzdakService.list(new LambdaQueryWrapper<SpcyDaDzdak>()
                .eq(SpcyDaDzdak::getFzzdaid, zzdaid)
                .isNotNull(SpcyDaDzdak::getFzzdaid));
        if (CollectionUtils.isNotEmpty(bdzzdaList)){
            return Result.error("当前纸质档案已经绑定，无法再次绑定该纸质档案！");
        }else {
            return Result.ok("操作成功");
        }
    }

    /**
     * 判断纸质档案绑定的电子档案是否已经并绑定
     */
    public Result verifyZzdaBdDzda(String dzdaid){
        List<SpcyDaZzdak> bdzzdaList = spcyDaZzdakService.list(new LambdaQueryWrapper<SpcyDaZzdak>()
                .eq(SpcyDaZzdak::getFdzdaid, dzdaid)
                .isNotNull(SpcyDaZzdak::getFdzdaid));
        if (CollectionUtils.isNotEmpty(bdzzdaList)){
            return Result.error("当前电子档案已经绑定，无法再次绑定该电子档案！");
        }else {
            return Result.ok("操作成功");
        }
    }

    /**
     * 删除-借阅申请流程
     * @param sqid 申请id
     */
    public void deleteJysqFlow(String sqid){
        SpcyDaSqlc sqlc = spcyDaSqlcService.getById(sqid);

        if (sqlc != null){
            String lognr = "删除借阅档案申请：";
            lognr += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(sqlc));

            spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM+"-借阅档案",lognr,sqlc.getFdaid(),SpcyConstant.LOGS_SYS_TYPE_SC,SpcyConstant.LOGS_SYS_TYPE_SC);
        }

    }

    /**
     * 判断电子档案-是否绑定纸质档案
     * @param dzdaid 电子档案
     * @return SpcyDaZzdak
     */
    public SpcyDaZzdak getZzdaByDzdaId(String dzdaid){
        SpcyDaDzdak dzda = spcyDaDzdakService.getById(dzdaid);
        if (dzda == null){
            return new SpcyDaZzdak();
        }
        if (StringUtils.isBlank(dzda.getFzzdaid())){
            return new SpcyDaZzdak();
        }

        SpcyDaZzdak zzda = spcyDaZzdakService.getById(dzda.getFzzdaid());
        if (zzda == null){
            return new SpcyDaZzdak();
        }

        return zzda;
    }

    /**
     * 获取电子档案-通过电子档案id
     */
    public SpcyDaDzdak getDzdaById(String id){
        SpcyDaDzdak dzda = spcyDaDzdakService.getById(id);
        return dzda;
    }

    /**
     * 获取纸质档案-通过纸质档案id
     */
    public SpcyDaZzdak getZzdaById(String id){
        SpcyDaZzdak zzda = spcyDaZzdakService.getById(id);
        return zzda;
    }

    /**
     * 电子档案-归档
     * @param ids 档案ids
     */
    public HttpResult gdDzda(String ids){
        if (StringUtils.isBlank(ids)){
            throw new BusinessException("归档参数不能为空,请联系管理员！");
        }

        List<String> idsList = Arrays.asList(ids.split(",|，"));
        if (CollectionUtils.isEmpty(idsList)){
            throw new BusinessException("归档参数错误,请联系管理员！");
        }

        List<SpcyDaDzdak> dzdaList = spcyDaDzdakService.listByIds(idsList);
        if (CollectionUtils.isEmpty(dzdaList)){
            throw new BusinessException("档案数据不存在，请刷新重试并联系管理员！");
        }

        for (SpcyDaDzdak dzdak : dzdaList){

            dzdak.setFsfgd(DaConstant.DZDA_GDZT_YES);
            spcyDaDzdakService.updateById(dzdak);

            String lognr = "电子档案-归档-档案名称["+dzdak.getFdamc()+"]和档案编号["+dzdak.getFdabh()+"]；";
            spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM,lognr,dzdak.getId(),SpcyConstant.LOGS_SYS_TYPE_BC,SpcyConstant.LOGS_SYS_TYPE_BC);

        }

        return HttpResult.ok("归档成功");
    }

    /**
     * 纸质档案入库
     * @param daid 档案id
     * @return
     */
    public HttpResult rkZzda(String daid,SpcyDaSqlc sqlc){
        SpcyDaZzdak zzda = spcyDaZzdakService.getById(daid);
        if (zzda == null){
            return HttpResult.error("当前档案数据不存在，请刷新重试并联系管理员！");
        }
        zzda.setFrkzt(DaConstant.ZZDA_RKZT_YJRK);
        spcyDaZzdakService.updateById(zzda);
        addRkjl(daid,zzda.getFdabh(),zzda.getFdamc(),null,sqlc); // 入库记录

        String lognr = "纸质档案入库-档案名称["+zzda.getFdamc()+"]和档案编号["+zzda.getFdabh()+"]；";
        spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM,lognr,daid,SpcyConstant.LOGS_SYS_TYPE_BC,SpcyConstant.LOGS_SYS_TYPE_BC);

        return HttpResult.ok("档案入库成功");
    }

    /**
     * 纸质档案出库
     * @param daid 档案id
     * @return
     */
    public HttpResult ckZzda(String daid,SpcyDaSqlc sqlc){
        SpcyDaZzdak zzda = spcyDaZzdakService.getById(daid);
        if (zzda == null){
            return HttpResult.error("当前档案数据不存在，请刷新重试并联系管理员！");
        }
        zzda.setFrkzt(DaConstant.ZZDA_RKZT_YCK);
        spcyDaZzdakService.updateById(zzda);

        this.addCkjl(daid,zzda.getFdabh(),zzda.getFdamc(),null,sqlc);

        String lognr = "纸质档案出库-档案名称["+zzda.getFdamc()+"]和档案编号["+zzda.getFdabh()+"]；";
        spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM,lognr,daid,SpcyConstant.LOGS_SYS_TYPE_BC,SpcyConstant.LOGS_SYS_TYPE_BC);

        return HttpResult.ok("档案出库成功");
    }

    /**
     *  借出-纸质档案
     */
    public HttpResult jczzDa(String fdaid,SpcyDaSqlc sqlc){
        if (StringUtils.isBlank(fdaid)){
            return HttpResult.error("档案参数丢失，请联系管理员！");
        }
        HttpResult result = this.ckZzda(fdaid,sqlc);// 出库
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("借出档案成功");
    }

    /**
     * 借出-借阅档案
     * @param sqid 申请id
     */
    public HttpResult jcjyda(String sqid){
        SpcyDaSqlc sqlc = spcyDaSqlcService.getById(sqid);
        if (sqlc == null){
            return HttpResult.error("当前申请借阅数据不存在，请刷新重试并联系管理员！");
        }

        String dzdaid = sqlc.getFdaid(); // 电子档案id
        SpcyDaDzdak dzda = spcyDaDzdakService.getById(dzdaid);
        if (dzda == null){
            String msg = "当前申请借阅数据中，";
            msg += "电子档案编号["+sqlc.getFdabh()+"]和电子档案名称["+sqlc.getFdamc()+"], 电子档案数据不存在，请联系管理员！";
            log.error(msg);
            return HttpResult.error(msg);
        }

        if (StringUtils.isNotBlank(sqlc.getFjyzt()) && sqlc.getFjyzt().equals(DaConstant.SQ_JYZT_DJC)) {
            String fzzdaid = dzda.getFzzdaid(); // 纸质档案id
            HttpResult result = this.jczzDa(fzzdaid,sqlc);// 借出纸质档案
            if (!result.isSuccess()) {
                log.error(result.getMessage());
                return HttpResult.error(result.getMessage());
            } else {
                sqlc.setFjyzt(DaConstant.SQ_JYZT_DYHGH); // 待用户归还
                spcyDaSqlcService.updateById(sqlc);
            }
        }

        return HttpResult.ok("借出档案成功");
    }

    /**
     * 归还-借阅档案
     * @param isGly 是否管理员(true,false)
     */
    public HttpResult ghDzJysq(String sqid,boolean isGly){
        SpcyDaSqlc sqlc = spcyDaSqlcService.getById(sqid);
        if (sqlc == null){
            return HttpResult.error("当前申请借阅数据不存在，请刷新重试并联系管理员！");
        }

        String dzdaid = sqlc.getFdaid(); // 电子档案id
        SpcyDaDzdak dzda = spcyDaDzdakService.getById(dzdaid);
        if (dzda == null){
            String msg = "当前申请借阅数据中，";
            msg += "电子档案编号["+sqlc.getFdabh()+"]和电子档案名称["+sqlc.getFdamc()+"], 电子档案数据不存在，请联系管理员！";
            log.error(msg);
            return HttpResult.error(msg);
        }

        if (StringUtils.isNotBlank(sqlc.getFjyzt()) && !sqlc.getFjyzt().equals(DaConstant.SQ_JYZT_DJC) && !sqlc.getFjyzt().equals(DaConstant.SQ_JYZT_WXJC)) {
            String fzzdaid = dzda.getFzzdaid(); // 纸质档案id
            HttpResult result = this.ghzzDa(fzzdaid,sqlc);// 归还纸质档案
            if (!result.isSuccess()){
                log.error(result.getMessage());
            }

            // 是否管理员操作
            if (isGly) {
                sqlc.setFjyzt(DaConstant.SQ_JYZT_YGH); // 已归还
                spcyDaSqlcService.updateById(sqlc);
            }else {
                sqlc.setFjyzt(DaConstant.SQ_JYZT_DQRGH); // 待确认归还
                spcyDaSqlcService.updateById(sqlc);
            }

        }

        return HttpResult.ok("归还档案成功");
    }

    /**
     * 确认归还-借阅档案
     */
    public HttpResult qrghJyda(String sqid){
        SpcyDaSqlc sqlc = spcyDaSqlcService.getById(sqid);
        if (sqlc == null){
            return HttpResult.error("当前申请借阅数据不存在，请刷新重试并联系管理员！");
        }
        String dzdaid = sqlc.getFdaid(); // 电子档案id
        sqlc.setFjyzt(DaConstant.SQ_JYZT_YGH); // 已归还
        spcyDaSqlcService.updateById(sqlc);

        if (StringUtils.isNotBlank(dzdaid)) {
            SpcyDaDzdak dzda = this.getDzdaById(dzdaid);
            if (dzda != null) {
                String fzzdaid = dzda.getFzzdaid(); // 纸质档案id
                this.ghzzDa(fzzdaid,sqlc);// 归还纸质档案
            }
        }

        return HttpResult.ok("确认归还");
    }

    /**
     * 归还纸质档案
     */
    public HttpResult ghzzDa(String fdaid,SpcyDaSqlc sqlc){
        if (StringUtils.isBlank(fdaid)){
            return HttpResult.error("档案参数丢失，请联系管理员！");
        }
        HttpResult result = this.rkZzda(fdaid,sqlc);// 入库
        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }

        return HttpResult.ok("归还档案成功");
    }

    /**
     * 通过纸质档案id-查询当前处于档案归还的借阅流程
     * @param fdaid 纸质档案id
     */
    public void getCurrentSqlcByZzdaid(String fdaid){

        // 先获取绑定该纸质档案的电子档案,有效并归档
        List<SpcyDaDzdak> dzdaList = spcyDaDzdakService.list(new LambdaQueryWrapper<SpcyDaDzdak>()
                .eq(SpcyDaDzdak::getFzzdaid, fdaid)
                .isNotNull(SpcyDaDzdak::getFzzdaid)
                .eq(SpcyDaDzdak::getFsfgd, DaConstant.DZDA_GDZT_YES)
                .eq(SpcyDaDzdak::getFyxzt, DaConstant.YXZT_YX)
                .orderByDesc(SpcyDaDzdak::getCreateTime));

        if (CollectionUtils.isNotEmpty(dzdaList)){
            SpcyDaDzdak dzda = dzdaList.get(0); // 电子档案

            // 对应的申请流程- 审批通过，处于 待确认归还、待用户归还
            List<SpcyDaSqlc> sqlcList = spcyDaSqlcService.list(new LambdaQueryWrapper<SpcyDaSqlc>()
                    .eq(SpcyDaSqlc::getFdaid, dzda.getId())
                    .eq(SpcyDaSqlc::getFspzt,DaConstant.SQ_SPZT_YTG)
                    .and(q -> q.eq(SpcyDaSqlc::getFjyzt, DaConstant.SQ_JYZT_DQRGH)
                            .or()
                            .eq(SpcyDaSqlc::getFjyzt, DaConstant.SQ_JYZT_DYHGH)
                    )
            );
            if (CollectionUtils.isNotEmpty(sqlcList)){
                for (SpcyDaSqlc sqlc : sqlcList){
                    this.qrghJyda(sqlc.getId()); // 归还纸质档案
                }
            }

        }

    }

    /**
     * 上传档案文件
     * @param file 文件
     * @param id  档案id
     * @return
     */
    public HttpResult uploadMoreTypeFile(MultipartFile file, String id){

        if (StringUtils.isEmpty(id)){
            return HttpResult.error("档案参数不能为空");
        }

        if (file == null) {
            return HttpResult.error("上传失败,请先选择文件");
        }

        SpcyDaWjjl wjjl = new SpcyDaWjjl();

        String originalFilename = file.getOriginalFilename();
        // 文件类型
        String wjlx = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 文件名
//        String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
        String sjmc = GetUuIdUtils.NotReplaceUuId();
        String wjccmc = sjmc + wjlx;
        String nowDay = DateUtil.today(); // 获取当前日期 yyyy-MM-dd
        String newDir = SpcyConstant.DA_Wjjl_File_Base_Url + "/" + nowDay;

//        localFileUploadUtils.uploadZdyFile(file, wjccmc, newDir); // 上传文件

        spcyMinioUtils.uploadFile(file,newDir); // 上传文件

        // 有效的档案
        List<SpcyDaWjjl> daList = spcyDaWjjlService.list(new LambdaQueryWrapper<SpcyDaWjjl>()
                .eq(SpcyDaWjjl::getFdaid, id)
                .eq(SpcyDaWjjl::getFyxzt, DaConstant.YXZT_YX));

        String Id = GetUuIdUtils.NotReplaceUuId();
        wjjl.setFwjmc(wjccmc);
        wjjl.setId(Id);
        wjjl.setFdaid(id);
        wjjl.setFwjlx(wjlx);
        String cflj = "/" + newDir + "/" + wjccmc;
        wjjl.setFwjlj(cflj);
        wjjl.setFyxzt(DaConstant.YXZT_YX); // 有效
        boolean save = spcyDaWjjlService.save(wjjl);
        if (save) {
            if (CollectionUtils.isNotEmpty(daList)){
                for (SpcyDaWjjl djjl : daList){
                    djjl.setFyxzt(DaConstant.YXZT_YSX); // 失效
                    spcyDaWjjlService.updateById(djjl);
                }
            }

            String czContent = "上传档案文件: ";
            czContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(wjjl));
            spcyLogService.addLogs(LogConstant.LOGS_DA_SYSTEM, czContent, id, SpcyConstant.LOGS_SYS_TYPE_BC, SpcyConstant.LOGS_SYS_TYPE_BC);
        }

        return HttpResult.ok("上传成功");
    }

    /**
     * 下载文件
     * @param id 档案id
     * @param response
     */
    public void getLocalFileByDaId(String id, HttpServletResponse response) {
        if (StringUtils.isEmpty(id)){
            log.error("档案参数为空");
            throw new BusinessException("档案参数为空");
        }

        // 有效文件
        List<SpcyDaWjjl> wjjl = spcyDaWjjlService.list(new LambdaQueryWrapper<SpcyDaWjjl>()
                .eq(SpcyDaWjjl::getFdaid,id)
                .eq(SpcyDaWjjl::getFyxzt,DaConstant.YXZT_YX)
                .orderByDesc(SpcyDaWjjl::getCreateTime));
        if (CollectionUtils.isEmpty(wjjl)){
            log.error("档案参数为空-{}的文件记录不存在",id);
            throw new BusinessException("档案参数为空-{}的文件记录不存在"+id);
        }
        String wjlj = wjjl.get(0).getFwjlj();
        if (StringUtils.isNotBlank(wjlj)) {
//            localFileUploadUtils.ejzLocalFileURlByZdy(wjlj, response); // 本地下载
            spcyMinioUtils.download(wjlj,response); // minio下载
        }else {
            throw new BusinessException("当前档案记录不存在文件，请先上传文件");
        }
    }

    /**
     * 获取档案文件
     * @param id 档案id
     */
    public SpcyDaWjjl getDaFileByDaId(String id){
        if (StringUtils.isEmpty(id)){
            log.error("档案参数为空");
            throw new BusinessException("档案参数为空");
        }

        // 有效文件
        List<SpcyDaWjjl> wjjl = spcyDaWjjlService.list(new LambdaQueryWrapper<SpcyDaWjjl>()
                .eq(SpcyDaWjjl::getFdaid,id)
                .eq(SpcyDaWjjl::getFyxzt,DaConstant.YXZT_YX)
                .orderByDesc(SpcyDaWjjl::getCreateTime));
        if (CollectionUtils.isEmpty(wjjl)){
            log.error("档案参数为-{}的文件记录不存在",id);
            throw new BusinessException("档案参数为-{}的文件记录不存在"+id);
        }

        return wjjl.get(0);
    }

    /**
     * 获取文件类型
     * @param id 档案id
     */
    public HttpResult getFileTypeByDaId(String id){
        if (StringUtils.isEmpty(id)){
            throw new BusinessException("档案id为空");
        }

        // 有效文件
        List<SpcyDaWjjl> wjjl = spcyDaWjjlService.list(new LambdaQueryWrapper<SpcyDaWjjl>()
                .eq(SpcyDaWjjl::getFdaid,id)
                .eq(SpcyDaWjjl::getFyxzt,DaConstant.YXZT_YX)
                .orderByDesc(SpcyDaWjjl::getCreateTime));
        if (CollectionUtils.isEmpty(wjjl)){
            throw new BusinessException("文件记录id为-{}的文件记录不存在"+id);
        }
        String wjlx = wjjl.get(0).getFwjlx();
        if (StringUtils.isNotBlank(wjlx)) {
            return HttpResult.ok(wjlx);
        }else {
            throw new BusinessException("当前档案记录不存在文件，请先上传文件");
        }
    }

    /**
     * 计算-借阅申请编码规则 SQ-20230812-00001
     */
    public String getJysqBhgz(){
        String sqbh = "";
        String bhQz = "SQ"; // 编号前缀
        String bhMiddle = DateConversionUtils.DateToYYYYMMDD(new Date());
        String bhQzAndMiddle = bhQz + "-" + bhMiddle + "-";  // 编号前半部分
        String bhhz = ""; // 编号后缀

        // 获取有多少申请数量
        List<SpcyDaSqlc> sizeList = spcyDaSqlcService.list(new LambdaQueryWrapper<SpcyDaSqlc>()
                .likeRight(SpcyDaSqlc::getFsqbh, bhQzAndMiddle));
        int total = sizeList.size();
        if (total > 0){
            total = total + 1;
            int nowBhLength = String.valueOf(total).length(); // 当前编号长度
            if (nowBhLength == 1){
                bhhz = "0000"+String.valueOf(total);
            }else if (nowBhLength == 2){
                bhhz = "000"+String.valueOf(total);
            }else if (nowBhLength == 3){
                bhhz = "00"+String.valueOf(total);
            }else if (nowBhLength == 4){
                bhhz = "0"+String.valueOf(total);
            }else {
                bhhz = String.valueOf(total);
            }
        }else {
            bhhz = "00001";
        }

        sqbh = bhQzAndMiddle + bhhz;

        return sqbh;
    }

}
